package com.saihai.jiankang.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.saihai.common.exception.ServiceException;
import com.saihai.common.utils.MessageUtils;
import com.saihai.common.utils.StringUtils;
import com.saihai.jiankang.userXs.domain.JkUserXs;
import com.saihai.jiankang.userXs.service.IJkUserXsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.saihai.common.annotation.Log;
import com.saihai.common.core.controller.BaseController;
import com.saihai.common.core.domain.AjaxResult;
import com.saihai.common.enums.BusinessType;
import com.saihai.jiankang.student.domain.UfXs;
import com.saihai.jiankang.student.service.IUfXsService;
import com.saihai.common.utils.poi.ExcelUtil;
import com.saihai.common.core.page.TableDataInfo;

/**
 * 学生信息Controller
 *
 * @author hzn
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/jiankang/xs")
public class UfXsController extends BaseController {
    @Autowired
    private IUfXsService ufXsService;

    @Autowired
    private IJkUserXsService jkUserXsService;

    /**
     * 查询学生信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UfXs ufXs) {
        startPage();
        List<UfXs> list = ufXsService.selectUfXsList(ufXs);
        return getDataTable(list);
    }

    /**
     * 查询学生信息列表
     */
    @GetMapping("/selectUfXsTreeList")
    public TableDataInfo selectUfXsTreeList(UfXs ufXs) {
        startPage();
        List<UfXs> list = ufXsService.selectUfXsTreeList(ufXs);
        return getDataTable(list);
    }

    /**
     * 根据班级查询学生
     */
    @GetMapping("/selectBjlist")
    public TableDataInfo selectBjlist(UfXs ufXs) {
        if (StringUtils.isNull(ufXs.getSzbjId())) {
            throw new ServiceException(MessageUtils.message("班级不能为空！"));
        }
        List<UfXs> list = ufXsService.selectUfXsList(ufXs);
        return getDataTable(list);
    }

    /**
     * 查询学生信息列表
     */
    @GetMapping("/selectJzXs")
    public TableDataInfo selectJzXs(UfXs ufXs) {
        List<UfXs> list = ufXsService.selectJzXs(ufXs);
        return getDataTable(list);
    }

    /**
     * 家长绑定学生
     * @param ufXs
     * @return
     */
    @PostMapping("/bindXszj")
    public AjaxResult bindXszj(@RequestBody UfXs ufXs) {
        UfXs ufXs1 = new UfXs();
        ufXs1.setXm(ufXs.getXm());
        ufXs1.setZjhm(ufXs.getZjhm());
        List<UfXs> list = ufXsService.selectUfXsList(ufXs1);
        if (list.size() == 0) {
            return AjaxResult.error("没找到该学生");
        }else {

            JkUserXs jkUserXs = new JkUserXs();

            jkUserXs.setJkUserId(ufXs.getRequestId());
            jkUserXs.setXsId(list.get(0).getId());
            List<JkUserXs> list1 = jkUserXsService.selectJkUserXsList(jkUserXs);
            if (list1.size() == 0) {
                jkUserXsService.insertJkUserXs(jkUserXs);
                return AjaxResult.success("绑定成功");
            }else {
                return AjaxResult.error("你已经绑定了该成员！");
            }

        }
    }


    /**
     * 获取学生信息详细信息 小程序端
     */
    @GetMapping(value = "/getStudentInfo/{id}")
    public AjaxResult getStudentInfo(@PathVariable("id") Long id) {
        return success(ufXsService.getStudentInfo(id));
    }

    /**
     * 导出学生信息列表
     */
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UfXs ufXs) {
        List<UfXs> list = ufXsService.selectUfXsList(ufXs);
        ExcelUtil<UfXs> util = new ExcelUtil<UfXs>(UfXs.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ufXsService.selectUfXsById(id));
    }

    /**
     * 新增学生信息
     */
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UfXs ufXs) {
        return toAjax(ufXsService.insertUfXs(ufXs));
    }

    /**
     * 修改学生信息
     */
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editStudent")
    public AjaxResult edit(@RequestBody UfXs ufXs) {
        return toAjax(ufXsService.updateUfXs(ufXs));
    }

    /**
     * 修改留校状态
     */
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editStudentLxzt")
    public AjaxResult editStudentLxzt(@RequestBody UfXs ufXs) {
        UfXs ufXs1 = new UfXs();
        ufXs1.setId(ufXs.getId());
        ufXs1.setLxzt(ufXs.getLxzt());
        return toAjax(ufXsService.updateUfXs(ufXs1));
    }

    /**
     * 解绑学生
     */
    @Log(title = "解绑学生", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ufXsService.deleteUfXsByIds(ids));
    }
}
